package com.h6.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.h6.crm.annotation.LoginUser;
import com.h6.crm.annotation.ResponseResult;
import com.h6.crm.service.ContractService;
import com.h6.crm.service.ProjectService;
import com.h6.crm.service.QuoteService;
import com.h6.crm.util.pojo.R;
import com.h6.crm.util.pojo.dto.FollowRecordDto;
import com.h6.crm.util.pojo.dto.QuoteDetailDto;
import com.h6.crm.util.pojo.dto.QuoteDto;
import com.h6.crm.util.pojo.vo.QueryProjectVO;
import com.h6.crm.util.pojo.vo.QueryQuoteReocrdVO;
import com.h6.crm.util.pojo.vo.QuoteApprovalVO;
import com.h6.crm.util.pojo.vo.QuoteVO;
import com.h6.crm.util.pojo.vo.ReportProjectVO;
import com.h6.crm.util.pojo.vo.SaveFollowVO;
import com.h6.crm.util.pojo.vo.UpdateProjectStatusVO;
import com.h6.crm.util.pojo.vo.UpdateQuoteStatusVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;

import com.h6.crm.entity.Contract;
import com.h6.crm.entity.Project;
import com.h6.crm.entity.SysUser;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "项目管理", description = "项目管理的API")
@RestController
@RequestMapping("/project")
@ResponseResult
public class ProjectController {

  @Autowired
  ProjectService projectService;

  @Autowired
  QuoteService quoteService;

  @Autowired
  ContractService contractService;

  @Operation(summary = "查询项目列表")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/list")
  public IPage<Project> queryProjectList(@Valid @RequestBody QueryProjectVO projectVo) {
    return projectService.queryProjectList(projectVo);
  }

  @Operation(summary = "查询项目名称")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/listName")
  public List<String> queryNameList(
      @Parameter(description = "项目名称", required = true) @RequestParam("projectName") String projectName) {
    return projectService.queryNameList(projectName);
  }

  @Operation(summary = "报备项目或者修改项目")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/report")
  public Boolean reportProject(@Valid @RequestBody ReportProjectVO reportProjectVO,
      @Parameter(hidden = true) @LoginUser SysUser user) {
    return projectService.reportProject(reportProjectVO, user);
  }

  @Operation(summary = "修改项目状态")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/{projectId}/status")
  public Boolean updateProjectStatus(
      @Parameter(description = "项目ID", required = true) @PathVariable("projectId") Long projectId,
      @Valid @RequestBody UpdateProjectStatusVO projectVO) {
    return projectService.updateProjectStatus(projectId, projectVO);
  }

  @Operation(summary = "项目跟进")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/follow/{projectId}")
  public Boolean saveFollow(@Parameter(description = "项目ID", required = true) @PathVariable("projectId") Long projectId,
      @Valid @RequestBody SaveFollowVO saveFollowVo) {
    projectService.saveFollow(projectId, saveFollowVo);
    return true;
  }

  @Operation(summary = "查询项目跟进记录列表")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/follow/{projectId}/list")
  public List<FollowRecordDto> queryFollowReocrdList(
      @Parameter(description = "项目ID", required = true) @PathVariable("projectId") Long projectId) {
    return projectService.queryFollowReocrdList(projectId);
  }

  @Operation(summary = "项目报价")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/quote/report/{projectId}")
  public Boolean quote(@Parameter(description = "项目ID", required = true) @PathVariable("projectId") Long projectId,
      @Valid @RequestBody QuoteVO quoteVO,
      @Parameter(hidden = true) @LoginUser SysUser user) {
    projectService.quote(projectId, quoteVO, user);
    return true;
  }

  @Operation(summary = "查询项目报价记录列表")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/quote/{projectId}/list")
  public List<QuoteDto> queryQuoteReocrdList(
      @Parameter(description = "项目ID", required = true) @PathVariable("projectId") Long projectId,
      @RequestBody QueryQuoteReocrdVO recordVo) {
    return projectService.queryQuoteReocrdList(projectId, recordVo);
  }

  @Operation(summary = "删除项目")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/{projectId}/delete")
  public Boolean deleteProject(
      @Parameter(description = "项目ID", required = true) @PathVariable("projectId") String projectId) {
    return projectService.removeById(projectId);
  }

  @Operation(summary = "根据ID查询项目详细信息")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/{projectId}")
  public Project getProjectDetail(
      @Parameter(description = "项目ID", required = true) @PathVariable("projectId") Long projectId) {
    return projectService.getProjectDetailById(projectId);
  }

  @Operation(summary = "根据ID查询报价详细信息")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/quote/{quoteId}")
  public QuoteDetailDto getQuoteRecordDetail(
      @Parameter(description = "报价记录ID", required = true) @PathVariable("quoteId") Long quoteId) {
    return quoteService.getQuoteRecordDetailById(quoteId);
  }

  @Operation(summary = "删除报价")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/quote/{quoteId}/delete")
  public Boolean deleteQuote(
      @Parameter(description = "报价记录ID", required = true) @PathVariable("quoteId") String quoteId) {
    return quoteService.removeById(quoteId);
  }

  @Operation(summary = "根据ID更改报价状态")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/quote/{quoteId}/status")
  public Boolean updateQuoteStatusById(
      @Parameter(description = "报价记录ID", required = true) @PathVariable("quoteId") Long quoteId,
      @Valid @RequestBody UpdateQuoteStatusVO quoteVO, @Parameter(hidden = true) @LoginUser SysUser user) {
    return quoteService.updateQuoteStatusById(quoteId, quoteVO, user);
  }

  @Operation(summary = "查询项目合同列表")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/contract/{projectId}/list")
  public List<Contract> queryContractList(
      @Parameter(description = "项目ID", required = true) @PathVariable("projectId") Long projectId) {
    return contractService.queryContractList(projectId);
  }

  @Operation(summary = "报价审批")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/quote/{quoteId}/approval")
  public Boolean quoteApproval(@Valid @RequestBody QuoteApprovalVO approvalVo,
      @Parameter(description = "报价ID", required = true) @PathVariable("quoteId") Long quoteId) {
    return quoteService.quoteApproval(quoteId, approvalVo);
  }

  @Operation(summary = "提交审批")
  @ApiResponses({
      @ApiResponse(responseCode = "200", content = {
          @Content(schema = @Schema(implementation = R.class), mediaType = "application/json") }) })
  @PostMapping("/quote/{quoteId}/submit")
  public Boolean quoteSubmit(@Parameter(description = "报价ID", required = true) @PathVariable("quoteId") Long quoteId) {
    return quoteService.quoteSubmit(quoteId);
  }
}
